home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / proff.zoo / lookup.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-07-04  |  3.3 KB  |  185 lines

  1. /*
  2.  * from K&R "The C Programming language"
  3.  * Table lookup routines
  4.  *
  5.  */
  6. #define INLOOK 1
  7.  
  8. #include <stdio.h>
  9. #include "proff.h"
  10.  
  11. struct
  12. lexlist    (*(*lextable))[];/* global pointer for lexical analyser hash table */
  13.  
  14. /*
  15.  * hash - for a hash valuue for string s
  16.  *
  17.  */
  18. int hash(s)
  19. char *s;
  20. {
  21.     int    hashval;
  22.  
  23.     for (hashval = 0; *s != '\0';)
  24.         hashval += *s++;
  25.     return (hashval % HASHMAX);
  26. }
  27.  
  28. /*
  29.  * lookup - lookup for a string s in the hash table
  30.  *
  31.  */
  32. struct hashlist
  33. *lookup(s, hashtab)
  34. char *s;
  35. struct hashlist *hashtab[];
  36. {
  37.     struct hashlist *np;
  38.  
  39.     for (np = hashtab[hash(s)]; np != NULL; np = np->next)
  40.         if (strcmp(s, np->name) == 0)
  41.             return(np);    /* found     */
  42.     return(NULL);        /* not found */
  43. }
  44.  
  45. /*
  46.  * install - install a string name in hashtable and its value def
  47.  * at a given hashtable.
  48.  */
  49. struct hashlist
  50. *install(name,def,hashtab)
  51. char *name;
  52. char *def;
  53. struct hashlist *hashtab[];
  54. {
  55.     int hashval;
  56.     struct hashlist *np, *lookup();
  57. #ifdef __GNUC__
  58.     char *strsave();
  59.     void *malloc(size_t);
  60. #else
  61.     char *strsave(), *malloc();
  62. #endif
  63.  
  64.     if ((np = lookup(name, hashtab)) == NULL) {    /* not found.. */
  65.         np = (struct hashlist *) malloc((size_t)sizeof(*np));
  66.         p_memoryus += sizeof(*np);
  67.         if (np == NULL)
  68.             return(NULL);
  69.         if ((np->name = strsave(name)) == NULL)
  70.             return(NULL);
  71.         hashval = hash(np->name);
  72.         np->next = hashtab[hashval];
  73.         hashtab[hashval] = np;
  74.     } else                    /* found..     */
  75.         free(np->def);            /* free prev.  */
  76.     if ((np->def = strsave(def)) == NULL)
  77.         return(NULL);
  78.     return(np);
  79. }
  80.  
  81. /*
  82.  * strsave - save string s somewhere
  83.  *
  84.  */
  85. char
  86. *strsave(s)
  87. char *s;
  88. {
  89. #ifdef __GNUC__
  90.     char *p;
  91.     void *malloc(size_t);
  92. #else
  93.     char *p, *malloc();
  94. #endif
  95.     register int n;
  96.  
  97.     n = (int)strlen(s) + 1;
  98.     if ((p = malloc((size_t)n)) != NULL) {
  99.         p_memoryus += n;
  100.         strcpy(p, s);
  101.     }
  102.     return(p);
  103. }
  104.  
  105. /*
  106.  * lexinstal - instal a string name in hashtable and its value
  107.  *           used by lexical analyser to quickly match a token
  108.  *           and return its lexical value.
  109.  *
  110.  */
  111. struct lexlist
  112. *lexinstal(name,val,flag,lextable)
  113. char *name;
  114. int val;
  115. int flag;
  116. struct lexlist *lextable[];
  117. {
  118.     int hashval;
  119.     struct lexlist *np,*lexlook();
  120. #ifdef __GNUC__
  121.     char *strsave();
  122.     void *malloc(size_t);
  123. #else
  124.     char *strsave(), *malloc();
  125. #endif
  126.  
  127.     if ((np = lexlook(name,lextable)) == NULL) {    /* not found.. */
  128.         np = (struct lexlist *) malloc((size_t)sizeof(*np));
  129.         p_memoryus += sizeof(*np);
  130.         if (np == NULL)
  131.             return(NULL);
  132.         if ((np->name = strsave(name)) == NULL)
  133.             return(NULL);
  134.         hashval = hash(np->name);
  135.         np->link = lextable[hashval];
  136.         lextable[hashval] = np;
  137.     }
  138.     np->val = val;                /* replace prev */
  139.     np->flag = flag;
  140.     return(np);
  141. }
  142.  
  143. /*
  144.  * lexlook - lookup for a string s in the hash table
  145.  *         used by lexinstal only.
  146.  *
  147.  */
  148. struct lexlist
  149. *lexlook(s,table)
  150. char *s;
  151. struct lexlist *table[];
  152. {
  153.     struct lexlist *np;
  154.  
  155.     for (np = table[hash(s)]; np != NULL; np = np->link)
  156.         if (strcmp(s, np->name) == 0)
  157.         return(np);    /* found     */
  158.     return(NULL);        /* not found */
  159. }
  160.  
  161. /*
  162.  * remove an item from the hash table forever
  163.  *
  164.  */
  165. struct lexlist
  166. *Remove(s, table)
  167. char *s;
  168. struct lexlist *table[];
  169. {
  170.     struct lexlist *np, *xp;
  171.  
  172.     np = table[hash(s)];
  173.     xp = np; 
  174.     while (np != NULL) {
  175.         if (strcmp(s, np->name) == 0) {
  176.             xp->link = np->link;    /* remove the link */
  177.             return(np);        /* return the lost */
  178.         }
  179.         xp = np; 
  180.         np = np->link;
  181.     }
  182.     return(NULL);
  183. }
  184.  
  185.